/**
 * Copyright &copy; 2013-2016 山东易科德软件有限公司   All rights reserved.
 */
package cn.net.ecode.modules.cms.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.net.ecode.common.persistence.Page;
import cn.net.ecode.common.service.CrudService;
import cn.net.ecode.modules.cms.dao.CategoryRoleDao;
import cn.net.ecode.modules.cms.dao.SiteDao;
import cn.net.ecode.modules.cms.entity.Category;
import cn.net.ecode.modules.cms.entity.CategoryRole;
import cn.net.ecode.modules.cms.entity.Site;

/**
 * 栏目授权Service
 * @author jiangwenchao
 * @version 2014-12-25
 */
@Service
@Transactional(readOnly = true)
public class CategoryRoleService extends CrudService<CategoryRoleDao, CategoryRole> {
	
	@Autowired
	private SiteDao siteDao;
	
	/**
	 * 获取单条数据
	 * @param categoryRole
	 * @return
	 */
	public CategoryRole get(CategoryRole categoryRole) {
		return super.get(categoryRole);
	}
	
	/**
	 * 查询列表数据
	 * @param categoryRole
	 * @return
	 */
	public List<CategoryRole> findList(CategoryRole categoryRole) {
		return super.findList(categoryRole);
	}
	
	/**
	 * 查询分页数据
	 * @param page 分页对象
	 * @param categoryRole
	 * @return
	 */
	public Page<CategoryRole> findPage(Page<CategoryRole> page, CategoryRole categoryRole) {
		return super.findPage(page, categoryRole);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param categoryRole
	 */
	@Transactional(readOnly = false)
	public void save(CategoryRole categoryRole) {
		dao.deleteByRole(categoryRole);
//		if (categoryRole.getCategoryIds() != null && categoryRole.getCategoryIds().size() > 0){
//			dao.insertBatch(categoryRole);
//		}
		// 分页批量插入数据（防止太长的sql数据库无法接受）
		/*final CategoryRole entity = categoryRole;
		List<String> list = categoryRole.getCategoryIds();
		Collections3.pageList(list, 100, new MethodCallback() {
			@SuppressWarnings("unchecked")
			public void execute(Object... objs) {

				// 执行批量插入数据
				CategoryRole e = new CategoryRole();
				e.setRole(entity.getRole());
				e.setCategoryIds((List<String>)objs[0]);
				dao.insertBatch(e);
				
			}
		});*/
	}
	
	/**
	 * 删除数据
	 * @param categoryRole
	 */
	@Transactional(readOnly = false)
	public void delete(CategoryRole categoryRole) {
		super.delete(categoryRole);
	}
	
	@Transactional(readOnly = false)
	public int deleteByRole(CategoryRole categoryRole) {
		return dao.deleteByRole(categoryRole);
	}

	public CategoryRole getSiteCategoryRole(CategoryRole categoryRole) {
		List<Site> siteList=siteDao.findList(new Site());
		for (Site site : siteList) {
			Category category=new Category();
			category.setSite(site);
			categoryRole.setSite(site);
			site.setCategoryList(dao.findCategoryBySite(category));
			site.setCategoryCodes(dao.findRoleCategory(categoryRole));
			//siteList.add(site);
		}
		categoryRole.setSiteList(siteList);
		return categoryRole;
	}
	
}